package com.frxuexi.park2212.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityParkEditBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/ParkEditActivity")
@AndroidEntryPoint
class ParkEditActivity : BaseActivity<ActivityParkEditBinding,ParkViewModel>() {
    var area=""
    var type=""
    var status=""
    @Autowired
    lateinit var bean:ParkBeanItem
    override fun initData() {
        vm.editPark.observe(this){
            ToastUtils.showLong("修改成功")
            finish()
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)

        binding.headParkAdd.headerTitle.setText("车位编辑")
        binding.headParkAdd.headerBack.setOnClickListener {
            finish()
        }
        binding.headParkAdd.headerAdd.apply {
            setImageResource(R.drawable.ic_action_ok)
            setOnClickListener {
                val remark = binding.parkaddRemark.text.toString()
                val carNo = binding.parkaddCarNum.text.toString()
                val user = binding.parkaddUser.text.toString()
                val item = ParkBeanItem(
                    bean.areaId, bean.areaName, bean.id, carNo, remark,
                    bean.spaceNumber, status, bean.spaceType, bean.userId, user
                )
                vm.editPark(bean.id!!,item)
            }
        }

        binding.parkaddCarNo.setText(bean.spaceNumber)
        binding.parkaddRemark.setText(bean.remarks)
        binding.parkaddUser.setText(bean.userName)
        binding.parkaddCarNum.setText(bean.licensePlate)

        if(bean.spaceStatus=="空闲"){
            binding.parkaddRg.check(R.id.rb_kx)
        }else if(bean.spaceStatus=="已占用"){
            binding.parkaddRg.check(R.id.rb_zy)
        }else{
            binding.parkaddRg.check(R.id.rb_wh)
        }

        val arr = mutableListOf<String>("A区停车场", "B区停车场", "C区停车场")
        if(!arr.contains(bean.areaName)){
            arr.add(bean.areaName)
        }

        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arr)
        binding.parkaddArea.adapter=adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

        binding.parkaddArea.setSelection(arr.indexOf(bean.areaName))

        binding.parkaddArea.onItemSelectedListener=object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area=arr[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

        val arrType = mutableListOf<String>("标准车位", "VIP车位")
        if(!arrType.contains(bean.spaceType)){
            arrType.add(bean.spaceType)
        }
        val adapter2 = ArrayAdapter(this, android.R.layout.simple_spinner_item, arrType)
        binding.parkaddType.adapter=adapter2
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

        binding.parkaddType.setSelection(arrType.indexOf(bean.spaceType))

        binding.parkaddType.onItemSelectedListener=object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type=arrType[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

        binding.parkaddRg.setOnCheckedChangeListener { group, checkedId ->
            if(checkedId==R.id.rb_kx){
                status="空闲"
            }else if(checkedId==R.id.rb_zy){
                status="已占用"
            }else{
                status="维护中"
            }
        }
    }

}